package Netty_basic;
import com.sun.tools.internal.ws.wsdl.document.soap.SOAPUse;
import io.netty.channel.DefaultEventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;

import java.util.concurrent.TimeUnit;

//EventLoop本质是一个单线程执行器（同时维护了一个Selector），里面有run方法处理Channel上源源不断的事件

public class element_EventLoop {
    public static void main(String[] args) throws InterruptedException {
        //test1();
        //test2();
        test3();
        System.out.println("---------------------------------------------");
    }

    public static void test1(){
        //相当于内部创建了两个EventLoop，每个EventLoop维护一个线程
        DefaultEventLoopGroup group=new DefaultEventLoopGroup(2);
        System.out.println(group.next());
        System.out.println(group.next());
        System.out.println(group.next());
    }

    public static void test2() throws InterruptedException {
        //处理普通任务
        NioEventLoopGroup nioEventLoopGroup=new NioEventLoopGroup(2);

        System.out.println("start");
        Thread.sleep(1000);
        nioEventLoopGroup.execute(()->{
            System.out.println("normal task");
        });
    }

    public static void test3(){
        //处理定时任务
        NioEventLoopGroup nioEventLoopGroup=new NioEventLoopGroup(2);

        System.out.println("start");
        nioEventLoopGroup.scheduleAtFixedRate(()->{
            System.out.println("I am running!");
        },0,1, TimeUnit.SECONDS);
    }


}
